new Vue({
	el: '#app',
	// 选项
	data: {
		id: '',
		aid: '',
		pid: '',
		uid: '',
		comment: '',
		list: [],
		current: [],
		path: '',
		page: '1',
		t: [],
		isShow: false
	},
	mounted: function() {
		this.init();
	},
	methods: {
		init: function() {
			this.getMain();
		},
		getMain: function() {
			var self = this;
			self.uid = $api.getStorage('uid');
			//self.getList();
			apiready = function() {
				var fid = api.pageParam;
				self.id = fid.id;
				//loading
				self.isS();

				api.addEventListener({
					name: 'scrolltobottom',
					extra: {
						threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
					}
				}, function(ret, err) {
					//alert('已滚动到底部');
					self.page++;
					if(self.page > self.pageCount) {
						api.toast({
							msg: '没有更多了',
							duration: 1000,
							location: 'bottom'
						});
					} else {
						self.getList();
					}
				});
			};
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});
				self.getList();
			}
		},
		//获得对话列表
		getList: function() {
			var self = this;
			var data = {
				id: self.id
			}
			$.post(base + config.sns_answer_comment_dialog, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.t = msg.comment.data;
					for(var i = 0; i < self.t.length; i++) {
						self.list.push(self.t[i]);
					};
					self.pageCount = msg.comment.page_count;
					self.current = msg.current;
					self.path = msg.url;
					self.isShow = true;
					self.isS();
				} else {
					self.isShow = true;
					self.isS();
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}

			}, 'json');
		},
		//无限下一级对话列表
		relodeone: function(id) {
			api.openFrame({
				name: 'comment-dialogue' + id,
				url: 'commentDialogue.html',
				vScrollBarEnabled: false,
				rect: {
					x: 0,
					y: 0,
					w: 'auto',
					h: 'auto'
				},
//				reload: true,
				pageParam: {
					id: id
				}
			});
		},
		//回复评论
		reply: function(pid, aid, pname) {
			var self = this;
			if(self.uid) {
				var el = $api.byId('answer');
				$api.removeCls(el, 'hide');
				this.comment = "";
				this.comment = "回复" + pname + "的评论：";
				this.pid = pid;
				this.aid = aid;
			} else {
				api.confirm({
					title: '尚未登录',
					msg: '是否前往登录页面',
					buttons: ['确定', '取消']
				}, function(ret, err) {
					var index = ret.buttonIndex;
					if(index == 1) {
						self.layout();
					}
				});
			}
		},
		//		发布我的评论
		sendComment: function() {
			var self = this;
			if(self.uid == "" || self.uid == null) {
				self.layout();
			} else {
				var data = {
					sns_answer_id: self.aid,
					content: self.comment,
					user_id: self.uid,
					parent_id: self.pid
				}
				$.post(base + config.sns_answer_comment_add, data, function(msg) {
					console.log(msg);
					if(msg.error == 0) {
						self.id = msg.id;
						self.comment = "";
						self.remove();
						self.list = [];
						self.getList();
					} else {
						console.log(msg.error_msg);
						api.toast({
							msg: msg.error_msg,
							duration: 2000,
							location: 'middle'
						});
					}
				}, 'json');
			}
		},
		//回复
		remove: function() {
			this.comment = "";
			var el = $api.byId('answer');
			$api.addCls(el, 'hide');
		},
		//登出
		layout: function() {
			var self = this;
			api.openWin({
				name: 'login',
				url: '../../html/login/login.html',
				vScrollBarEnabled: false,
//				reload: true,
				pageParam: {
					openName: 'comment-dialogue',
					id: self.id,
					type:1
				}
			});
		}
	}
})